

var api = require('../../utils/api.js');
var app = getApp();


Page({

    /**
     * 页面的初始数据
     */
    data: {
        sexs:['女','男'],
        array:['非目标客户','目标客户'],
        arrayb:['合作客户','待合作客户','掉客'],
        images:[],
        img:'',
        address:'',
        latitude: '',
        longitude: '',
        mlatitude: '',
        mlongitude: '',
        csex_t:'',
        msex_t:'',
        rivals:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getmyaddress();
        this.getallrival();
    },

    //d对手信息
    getallrival(){
        api.post({
            url: 'client/getallrival',
            data: {},
            success: data => {
               console.log(data);

               this.setData({
                 rivals: data.data
               })

            }
        });
    },
    bindPickerChangerival(ret){
        var value=ret.detail.value;
        var array=this.data.rivals;

        var v=array[value];

        this.setData({
            rival_name:v.name
        })
    },




    getmyaddress(){
        var that=this;
        wx.getLocation({
            type: 'gcj02', 
            success: function(res) {
              console.log('定位成功', res.latitude, res.longitude);

              that.setData({
                mlatitude:res.latitude,
                mlongitude:res.longitude
              })
        
            },
            fail: function(err) {
              console.error('定位失败', err);

            }
          });
    },
    onshow(res){
    
     console.log('-------',res); 
            

    },

    mapOpen() {
        let that = this
        wx.chooseLocation({
          latitude: that.data.lat,
          longitude: that.data.lng,
          success(res) {
              // 选择位置返回的信息
            console.log('选择位置返回的信息',res)
          }
        })
      },

    //x选择地址, longitude: 104.141478}
    getaddress(){
        console.log('---')
        var that=this;

  

        wx.chooseLocation({
            latitude: this.data.mlatitude,
            longitude:this.data.mlongitude,
            success(res) {
                // 选择位置返回的信息
             that.setData({
                 address:res.address,
                 latitude:res.latitude,
                 longitude:res.longitude
             })
            }
          })

    },
    getimgs(){
        let that = this;
        var usrl=api.HOST;

        wx.chooseMedia({
          count: 1,
          mediaType:['image'],
          success: function (res) {
            console.log(res)
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFiles;
            wx.uploadFile({
              filePath: tempFilePaths[0].tempFilePath,
              name: 'file',
              url: usrl+'/api/common/upload',
              formData:{},
              success:function(res){
                  console.log(res);
                var data= JSON.parse(res.data);
                that.setData({
                  img:data.data.fullurl
                });
        
              },
              fail: function (res) {
                console.log('上传失败');
                }
            })
          }
        })
    },
      
    getallimgs(){
        let that = this;
        var usrl=api.HOST;
        var images=this.data.images;

        wx.chooseMedia({
          count: 9,
          mediaType:['image'],
          success: function (res) {
            console.log(res)
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            let tempFilePaths = res.tempFiles;

            tempFilePaths.forEach(function(item){

                wx.uploadFile({
                    filePath: item.tempFilePath,
                    name: 'file',
                    url: usrl+'/api/common/upload',
                    formData:{},
                    success:function(res){
                        console.log(res);
                      var data= JSON.parse(res.data);
                      images.push(data.data.fullurl)
                      that.setData({
                        images:images
                      });
              
                    },
                    fail: function (res) {
                      console.log('上传失败');
                      }
                  })
            })

          
          }
        })
    },

    deleteimg(ret){
        console.log(ret);
        var i=ret.currentTarget.dataset.i;
        var images=this.data.images;
        var newimg=[];

        images.forEach(function(item,index){

            console.log(index);

            if(index!=i){
                newimg.push(item);
            }


        })

        this.setData({
            images:newimg
        })



    },

    formSubmit(e) {

        var that=this;

        var data=e.detail.value;

        data.wimages=this.data.images;
        data.image=this.data.img;
        data.lat=this.data.latitude;
        data.lng=this.data.longitude;
        data.rival_name=this.data.rival_name;

        api.post({
            url: 'client/submit',
            data: data,
            success: data => {
               console.log(data);

               wx.showToast({
                 title: data.msg,
                 icon:'none'
               })

               if(data.code==1){
                   wx.switchTab({
                     url: '/pages/list/index'
                   })
               }

            }
        });


        console.log(data)
    },

    setcsex(ret){
        console.log(ret);

        var value=ret.detail.value;
        
        this.setData({
            csex_t:value==1?'男':'女'
        })

    },
    setmsex(ret){
        console.log(ret);

        var value=ret.detail.value;
        
        this.setData({
            msex_t:value==1?'男':'女'
        })

    },

    cbindPickerChange(ret){

        var value=ret.detail.value;
        var array=this.data.arrayb;

        var v=array[value];
        this.setData({
            ctypes_t:v
            
        })
    },
    
    
    bindPickerChange(ret){
        var value=ret.detail.value;
        var array=this.data.array;

        var v=array[value];

        this.setData({
            types_t:v
            
        })


        

    }
    

})